/*
 * @Author: along
 * @Date: 2020-02-15
 */
<template>
    <div class="container kfProfit">
        <!-- header -->
        <div class="header-nav">
            <div class="header-left">
                <div class="header-box-content">
                    <p class="header-box-content-title">企业名称搜索:</p>
                    <el-input
                        v-model="head.company_name"
                        placeholder="请输入要搜索的内容"
                        suffix-icon="el-icon-search"
                        size="small"
                        clearable
                        class="search-ipt"
                        @change="getTableData(1)"
                        @keyup.native.enter="getTableData(1)"
                    />
                </div>
                <div class="header-box-content">
                    <p class="header-box-content-title">店铺搜索:</p>
                    <el-input
                        v-model="head.shop_title"
                        placeholder="请输入要搜索的内容"
                        suffix-icon="el-icon-search"
                        size="small"
                        clearable
                        class="search-ipt"
                        @change="getTableData(1)"
                        @keyup.native.enter="getTableData(1)"
                    />
                </div>
                <div class="header-box-content">
                    <p class="header-box-content-title">利润总额:</p>
                    <el-input
                        v-model="head.profit_range_start"
                        size="small"
                        clearable
                        class="search-ipt"
                        @change="getTableData(1)"
                        @keyup.native.enter="getTableData(1)"
                        style="width:100px"
                    />
                    <span style="margin: 0 3px">-</span>
                    <el-input
                        v-model="head.profit_range_end"
                        suffix-icon="el-icon-search"
                        size="small"
                        clearable
                        class="search-ipt"
                        @change="getTableData(1)"
                        @keyup.native.enter="getTableData(1)"
                        style="width:100px"
                    />
                </div>
            </div>
            <!-- <div class="header-right">
            </div> -->
        </div>
        <!-- /header -->

        <!-- table -->
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            @sort-change="handleSortChanged"
            border
            ref="kfProfit"
        >
            <el-table-column
                type="index"
                width="80"
                label="序号"
                align="center"
            />
            <el-table-column
                prop="company_name"
                label="客户企业名称"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                :width="110"
            />
            <el-table-column
                prop="shop_title"
                label="店铺名称"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                prop="pay_total"
                label="销售额"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                prop="profit_total"
                label="利润总额"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                sortable="custom"
            />
            <el-table-column
                prop="profit_today"
                label="今日利润"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                sortable="custom"
            />
            <el-table-column
                prop="profit_yesterday"
                label="昨日利润"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                sortable="custom"
            />
            <el-table-column
                prop="profit_week"
                label="本周利润"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                sortable="custom"
            />
            <el-table-column
                prop="profit_month"
                label="本月利润"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                sortable="custom"
            />
            <el-table-column
                prop="goods_num"
                label="商品总数"
                show-overflow-tooltip
                align="center"
            />
            <el-table-column
                label="操作"
                align="center"
                fixed="right"
                :width="120"
            >
                <template slot-scope="scope">
                    <el-button
                        type="text"
                        class="curse-add"
                        @click="fnShowProfit(scope.row)"
                    >
                        查看利润记录
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- /table -->

        <!-- 客户利润监控 -->
        <el-dialog
            width="730px"
            custom-class="schedule-dialog"
            :visible.sync="showDilog"
        >
            <el-table
                :data="tableDataProfit"
                height="500px"
                style="width: 100%;height:500px;max-height:500px"
                border
                ref="push"
            >
                <el-table-column
                    prop="goods_title"
                    label="商品名称"
                    show-overflow-tooltip
                    :formatter="emptyFormatter"
                    align="center"
                />
                <el-table-column
                    prop="goods_type"
                    label="商品类型"
                    show-overflow-tooltip
                    :formatter="emptyFormatter"
                    align="center"
                >
                    <template slot-scope="scope">
                        <p v-if="scope.row.goods_type == 1">自营</p>
                        <p v-else-if="scope.row.goods_type == 2">淘客</p>
                        <p v-else-if="scope.row.goods_type == 3">秒杀</p>
                        <p v-else-if="scope.row.goods_type == 4">商圈</p>
                        <p v-else-if="scope.row.goods_type == 5">新人专区</p>
                        <p v-else>--</p>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="order_time"
                    label="交易时间"
                    align="center"
                    show-overflow-tooltip
                    :formatter="emptyFormatter"
                    :width="150"
                >
                    <template slot-scope="scope">
                        {{ scope.row.order_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }}
                    </template>
                </el-table-column>
                <el-table-column
                    prop="pay_price"
                    label="成交价格"
                    show-overflow-tooltip
                    :formatter="emptyFormatter"
                    align="center"
                />
                <el-table-column
                    prop="cost_price"
                    label="成本价"
                    show-overflow-tooltip
                    :formatter="emptyFormatter"
                    align="center"
                />
                <el-table-column
                    prop="profit_money"
                    label="利润"
                    show-overflow-tooltip
                    :formatter="emptyFormatter"
                    align="center"
                />
            </el-table>
            <div class="pagination-wrapper clearfix">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page.sync="profit_page.now_page"
                    :total="profit_page.total_count"
                    :page-size="profit_page.page_size"
                    @current-change="fnGetProfit"
                />
            </div>
        </el-dialog>
        <!-- /客户利润监控 -->

        <!-- /分页 -->
        <div class="pagination-wrapper clearfix">
            <el-pagination
                background
                layout="total, prev, pager, next"
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                @current-change="getTableData"
            />
        </div>
    </div>
</template>

<script>
import { back } from '@/mixin/back';
export default {
    mixins: [back],
    data () {
        return {
            shop_no: '',
            showDilog: false,
            tableDataProfit: [],
            head: {
                company_name: '',
                shop_title: '',
                profit_range_start: '',
                profit_range_end: ''
            },
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            profit_page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            tableData: [],
            authResult: {},
            filterData: {
                sort_field: 0,
                sort_type: 0
            },
        };
    },
    filters: {},
    computed: {},
    watch: {},
    mounted () {
        this.authResult = this.$route.meta.authResult;
        this.fnBreadcrumb();
        this.getTableData();
    },
    methods: {
        /**
         * 设置表单header
         */
        fnBreadcrumb () {
            this.$store.commit('SET_HEADER', [{ title: "APP后台"},{title: '客户利润监控'}]);
        },

        /**
         * 获取表格数据
         */
        getTableData (page) {
            this.$post('/student/shop_profit%5Cget_list', {
                page_id: page || this.page.now_page,
                company_name: this.head.company_name,
                shop_title: this.head.shop_title,
                profit_range: this.head.profit_range_start + '-' + this.head.profit_range_end,
                sort_field: this.filterData.sort_field,
                sort_type: this.filterData.sort_type
            }, resp => {
                if (resp.code == 1) {
                    this.tableData = resp.data.list;
                    this.page.now_page = resp.data.page.now_page;
                    this.page.total_count = resp.data.page.total_count;
                    this.page.page_size = resp.data.page.page_size;
                }
            });
        },

        /**
         * 查看利润记录
         */
        fnShowProfit (row) {
            this.showDilog = !this.showDilog;
            this.shop_no = row.shop_no;
            this.fnGetProfit(1);
        },

        /**
         * 利润数据获取
         */
        fnGetProfit (page) {
            this.$post('/student/shop_profit%5Cget_log_list',{
                shop_no: this.shop_no,
                page_id: page || this.page.now_page,
            }, resp => {
                if (resp.code == 1) {
                    this.tableDataProfit = resp.data.list;
                    this.profit_page.now_page = resp.data.page.now_page;
                    this.profit_page.total_count = resp.data.page.total_count;
                    this.profit_page.page_size = resp.data.page.page_size;
                } else {
                    this.$message({
                        type: 'waring',
                        message: resp.msg,
                        center: true
                    });
                }
            });
        },

        /**
         * 排序
         */
        handleSortChanged(data) {
            let sortTypeMap = {
                ascending: 'asc',
                descending: 'desc'
            };

            let sortFieldMap = {
                profit_total: 'profit_total',
                profit_month: 'profit_month',
                profit_week: 'profit_week',
                profit_yesterday: 'profit_yesterday',
                profit_today: 'profit_today'
            };

            if (data.order) {
                this.filterData.sort_field = sortFieldMap[data.prop];
                this.filterData.sort_type = sortTypeMap[data.order];
            } else {
                this.filterData.sort_field = 'asc';
                this.filterData.sort_type = 'desc';
            }

            this.getTableData();
        },

        /**
         * 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        }
    }
};
</script>

<style lang="less" scoped>
    .container {
        background-color: #fff;
        padding: 10px 15px 5px 15px;
        display: flex;
        flex-direction: column;
        position: relative;
        width: 100%;
        .header-nav {
            position: relative;
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            justify-content: space-between;
            .header-left, .header-right {
                display: flex;
                align-items: center;
            }
            .header-box-content {
                display: flex;
                margin-right: 15px;
                align-items: center;
                color: #606266;
                margin-bottom: 10px;
                &-title {
                    margin-right: 2px;
                    white-space: nowrap;
                }
            }
        }
        /* 表单 */
        .scope-price {
            width: 50px;
            height: 50px;
        }
        /* 字体高亮 */
        .cursor-font {
            color: rgb(64, 158, 255);
            cursor: pointer;
        }
        /* 分页 */
        .pagination-wrapper {
            padding-top: 10px;
            padding-bottom: 10px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
    }
    /* 排序 */
    .sort-wrap {
        display: flex;
        align-items: center;
        cursor: pointer;
        .sort-spliter {
            width: 1px;
            height: 12px;
            background: rgba(235, 235, 235, 1);
            margin: 0 10px;
            }
            .btn-sort {
            font-size: 14px;
            line-height: 1;
            color: #ccc;
            &:hover {
                color: #20a0ff;
            }
        }
    }
    .el-button-left {
        margin-left: 40px;
    }
    .el-button-right {
        margin-right: 10px;
    }
</style>
<style lang="less">
    .kfProfit .el-table__body-wrapper .el-table__body .el-table__row td {
        border-right: none;
    }
    .kfProfit .el-table__header-wrapper .el-table__header .has-gutter tr th {
        border-right: none;
    }
    .kfProfit .el-table .el-table_1_column_6  .cell {
        position: relative;
    }
    .kfProfit .el-range-editor--small .el-range-separator{
        padding: 0 1px !important;
    }
</style>